<template>
  <div class="activities-container">
    <div class="page-header">
      <h1>活动管理</h1>
      <button class="add-button" @click="showAddActivityDialog = true">
        <i class="fa fa-plus"></i>
        添加活动
      </button>
    </div>
    
    <!-- 搜索和筛选 -->
    <div class="search-filter">
      <div class="search-box">
        <i class="fa fa-search"></i>
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索活动名称、描述"
          @input="handleSearch"
        />
      </div>
      <div class="filter-group">
        <select v-model="filterStatus" @change="handleFilter">
          <option value="">全部状态</option>
          <option value="active">进行中</option>
          <option value="upcoming">即将开始</option>
          <option value="ended">已结束</option>
        </select>
        <select v-model="filterType" @change="handleFilter">
          <option value="">全部类型</option>
          <option value="online">线上活动</option>
          <option value="offline">线下活动</option>
        </select>
      </div>
    </div>
    
    <!-- 活动列表 -->
    <div class="activities-table-container">
      <table class="activities-table">
        <thead>
          <tr>
            <th><input v-model="selectAll" type="checkbox" @change="toggleSelectAll" /></th>
            <th>ID</th>
            <th>活动名称</th>
            <th>类型</th>
            <th>状态</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>参与人数</th>
            <th>创建时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="activity in paginatedActivities" :key="activity.id">
            <td><input v-model="selectedActivities" type="checkbox" :value="activity.id" /></td>
            <td>{{ activity.id }}</td>
            <td class="activity-name">{{ activity.name }}</td>
            <td>
              <span class="type-badge" :class="activity.type">
                {{ activity.type === 'online' ? '线上活动' : '线下活动' }}
              </span>
            </td>
            <td>
              <span class="status-badge" :class="activity.status">
                {{ getStatusText(activity.status) }}
              </span>
            </td>
            <td>{{ formatDate(activity.startTime) }}</td>
            <td>{{ formatDate(activity.endTime) }}</td>
            <td>{{ activity.participants }}</td>
            <td>{{ formatDate(activity.createdAt) }}</td>
            <td class="action-buttons">
                <button class="view-button" @click="viewActivity(activity)">
                  <i class="fa fa-eye"></i>
                  查看
                </button>
                <button class="edit-button" @click="editActivity(activity)">
                  <i class="fa fa-edit"></i>
                  编辑
                </button>
                <button class="status-button" :class="activity.status" @click="changeStatus(activity)">
                  <i class="fa fa-power-off"></i>
                  状态
                </button>
                <button class="delete-button" @click="deleteActivity(activity)">
                  <i class="fa fa-trash"></i>
                  删除
                </button>
              </td>
          </tr>
        </tbody>
      </table>
      
      <!-- 空状态 -->
      <div v-if="filteredActivities.length === 0" class="empty-state">
        <i class="fa fa-search"></i>
        <p>暂无活动数据</p>
      </div>
    </div>
    
    <!-- 分页 -->
    <div v-if="filteredActivities.length > 0" class="pagination">
      <button :disabled="currentPage === 1" @click="currentPage = 1">
        <i class="fa fa-angle-double-left"></i>
      </button>
      <button :disabled="currentPage === 1" @click="currentPage--">
        <i class="fa fa-angle-left"></i>
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页
      </span>
      <button :disabled="currentPage === totalPages" @click="currentPage++">
        <i class="fa fa-angle-right"></i>
      </button>
      <button :disabled="currentPage === totalPages" @click="currentPage = totalPages">
        <i class="fa fa-angle-double-right"></i>
      </button>
    </div>
    
    <!-- 添加/编辑活动对话框 -->
    <div v-if="showAddActivityDialog || showEditActivityDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog" @click.stop>
        <div class="dialog-header">
          <h3>{{ showEditActivityDialog ? '编辑活动' : '添加活动' }}</h3>
          <button class="close-button" @click="closeDialog">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div class="dialog-content">
          <form @submit.prevent="submitActivityForm">
            <div class="form-group">
              <label>活动名称</label>
              <input 
                v-model="activityForm.name" 
                type="text" 
                required
                placeholder="请输入活动名称"
              />
            </div>
            <div class="form-group">
              <label>活动描述</label>
              <textarea 
                v-model="activityForm.description" 
                rows="3"
                placeholder="请输入活动描述"
              ></textarea>
            </div>
            <div class="form-group">
              <label>活动类型</label>
              <select v-model="activityForm.type" required>
                <option value="online">线上活动</option>
                <option value="offline">线下活动</option>
              </select>
            </div>
            <div class="form-group">
              <label>活动状态</label>
              <select v-model="activityForm.status" required>
                <option value="upcoming">即将开始</option>
                <option value="active">进行中</option>
                <option value="ended">已结束</option>
              </select>
            </div>
            <div class="form-group">
              <label>开始时间</label>
              <input 
                v-model="activityForm.startTime" 
                type="datetime-local" 
                required
              />
            </div>
            <div class="form-group">
              <label>结束时间</label>
              <input 
                v-model="activityForm.endTime" 
                type="datetime-local" 
                required
              />
            </div>
            <div class="form-group">
              <label>活动地点</label>
              <input 
                v-model="activityForm.location" 
                type="text" 
                placeholder="线上活动可填直播间链接"
              />
            </div>
            <div class="dialog-footer">
              <button type="button" class="cancel-button" @click="closeDialog">取消</button>
              <button type="submit" class="save-button">{{ showEditActivityDialog ? '更新' : '添加' }}</button>
            </div>
          </form>
        </div>
      </div>
    </div>
    
    <!-- 查看活动详情对话框 -->
    <div v-if="selectedActivity" class="dialog-overlay" @click="selectedActivity = null">
      <div class="dialog dialog-large" @click.stop>
        <div class="dialog-header">
          <h3>活动详情</h3>
          <button class="close-button" @click="selectedActivity = null">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div class="dialog-content">
          <div class="activity-detail">
            <h2>{{ selectedActivity.name }}</h2>
            <p class="activity-desc">{{ selectedActivity.description }}</p>
            <div class="detail-grid">
              <div class="detail-item">
                <span class="detail-label">活动类型：</span>
                <span class="type-badge" :class="selectedActivity.type">
                  {{ selectedActivity.type === 'online' ? '线上活动' : '线下活动' }}
                </span>
              </div>
              <div class="detail-item">
                <span class="detail-label">活动状态：</span>
                <span class="status-badge" :class="selectedActivity.status">
                  {{ getStatusText(selectedActivity.status) }}
                </span>
              </div>
              <div class="detail-item">
                <span class="detail-label">开始时间：</span>
                <span>{{ formatDate(selectedActivity.startTime) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">结束时间：</span>
                <span>{{ formatDate(selectedActivity.endTime) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">活动地点：</span>
                <span>{{ selectedActivity.location || '线上活动' }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">参与人数：</span>
                <span>{{ selectedActivity.participants }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">创建时间：</span>
                <span>{{ formatDate(selectedActivity.createdAt) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ActivitiesView',
  data() {
    return {
      // 模拟活动数据
      activities: [
        {
          id: 'activity1',
          name: 'Vue3 技术分享会',
          description: '深入探讨Vue3的新特性和最佳实践',
          type: 'online',
          status: 'active',
          startTime: '2024-01-15T19:00:00',
          endTime: '2024-01-15T21:00:00',
          location: 'B站直播间：https://live.bilibili.com/123456',
          participants: 5234,
          createdAt: '2024-01-01T10:00:00Z',
        },
        {
          id: 'activity2',
          name: '前端开发者大会',
          description: '2024年前端技术趋势与创新分享',
          type: 'offline',
          status: 'upcoming',
          startTime: '2024-02-10T09:00:00',
          endTime: '2024-02-11T17:00:00',
          location: '上海国际会议中心',
          participants: 1234,
          createdAt: '2024-01-02T14:30:00Z',
        },
        {
          id: 'activity3',
          name: 'TypeScript 实战训练营',
          description: '从零开始学习TypeScript高级特性',
          type: 'online',
          status: 'ended',
          startTime: '2024-01-05T14:00:00',
          endTime: '2024-01-05T16:00:00',
          location: '腾讯会议：876-543-210',
          participants: 876,
          createdAt: '2024-01-03T09:15:00Z',
        },
        {
          id: 'activity4',
          name: '前端性能优化工作坊',
          description: '实战演练前端性能优化技巧',
          type: 'offline',
          status: 'active',
          startTime: '2024-01-20T10:00:00',
          endTime: '2024-01-20T16:00:00',
          location: '北京中关村创新中心',
          participants: 234,
          createdAt: '2024-01-04T16:45:00Z',
        },
      ],
      searchQuery: '',
      filterStatus: '',
      filterType: '',
      selectedActivities: [],
      selectAll: false,
      showAddActivityDialog: false,
      showEditActivityDialog: false,
      selectedActivity: null,
      currentPage: 1,
      itemsPerPage: 10,
      activityForm: {
        id: '',
        name: '',
        description: '',
        type: 'online',
        status: 'upcoming',
        startTime: '',
        endTime: '',
        location: '',
      },
    }
  },
  computed: {
    filteredActivities() {
      let filtered = this.activities
      
      // 搜索
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase()
        filtered = filtered.filter(activity => 
          activity.name.toLowerCase().includes(query) || 
          activity.description.toLowerCase().includes(query),
        )
      }
      
      // 状态筛选
      if (this.filterStatus) {
        filtered = filtered.filter(activity => activity.status === this.filterStatus)
      }
      
      // 类型筛选
      if (this.filterType) {
        filtered = filtered.filter(activity => activity.type === this.filterType)
      }
      
      return filtered
    },
    paginatedActivities() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.filteredActivities.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.filteredActivities.length / this.itemsPerPage)
    },
  },
  methods: {
    handleSearch() {
      this.currentPage = 1
    },
    handleFilter() {
      this.currentPage = 1
    },
    toggleSelectAll() {
      if (this.selectAll) {
        this.selectedActivities = this.filteredActivities.map(activity => activity.id)
      } else {
        this.selectedActivities = []
      }
    },
    formatDate(dateString) {
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN')
    },
    getStatusText(status) {
      switch (status) {
        case 'active': return '进行中'
        case 'upcoming': return '即将开始'
        case 'ended': return '已结束'
        default: return '未知'
      }
    },
    viewActivity(activity) {
      this.selectedActivity = { ...activity }
    },
    editActivity(activity) {
      this.activityForm = { ...activity }
      this.showEditActivityDialog = true
    },
    changeStatus(activity) {
      const statusOptions = ['upcoming', 'active', 'ended']
      const currentIndex = statusOptions.indexOf(activity.status)
      const nextIndex = (currentIndex + 1) % statusOptions.length
      activity.status = statusOptions[nextIndex]
    },
    deleteActivity(activity) {
      if (confirm(`确定要删除活动「${activity.name}」吗？此操作不可撤销！`)) {
        const index = this.activities.findIndex(a => a.id === activity.id)
        if (index > -1) {
          this.activities.splice(index, 1)
        }
      }
    },
    closeDialog() {
      this.showAddActivityDialog = false
      this.showEditActivityDialog = false
      this.resetForm()
    },
    resetForm() {
      this.activityForm = {
        id: '',
        name: '',
        description: '',
        type: 'online',
        status: 'upcoming',
        startTime: '',
        endTime: '',
        location: '',
      }
    },
    submitActivityForm() {
      if (this.showEditActivityDialog) {
        // 编辑活动
        const index = this.activities.findIndex(a => a.id === this.activityForm.id)
        if (index > -1) {
          this.activities[index] = { ...this.activities[index], ...this.activityForm }
        }
      } else {
        // 添加活动
        const newActivity = {
          ...this.activityForm,
          id: 'activity' + (this.activities.length + 1),
          participants: 0,
          createdAt: new Date().toISOString(),
        }
        this.activities.push(newActivity)
      }
      
      this.closeDialog()
    },
  },
}
</script>

<style scoped>
.activities-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  overflow-x: auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.page-header h1 {
  color: #2c3e50;
  margin: 0;
  font-size: 24px;
}

.add-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
}

.add-button:hover {
  background: #2980b9;
}

.add-button i {
  margin-right: 5px;
}

/* 搜索和筛选 */
.search-filter {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 20px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  min-width: 300px;
}

.search-box i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
}

.search-box input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.filter-group {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.filter-group select {
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  background: white;
  min-width: 150px;
}

/* 活动列表 */
.activities-table-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  overflow-x: scroll;
  margin-bottom: 20px;
  min-width: 800px;
}

.activities-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 800px;
}

.activities-table th,
.activities-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
  white-space: nowrap;
}

.activities-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.activity-name {
  font-weight: 500;
  max-width: 200px;
  white-space: normal;
  word-break: break-word;
}

.type-badge,
.status-badge {
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.type-badge.online {
  background: #3498db;
  color: white;
}

.type-badge.offline {
  background: #2ecc71;
  color: white;
}

.status-badge.active {
  background: #2ecc71;
  color: white;
}

.status-badge.upcoming {
  background: #f39c12;
  color: white;
}

.status-badge.ended {
  background: #95a5a6;
  color: white;
}

.action-buttons {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.action-buttons button {
  padding: 3px 8px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.3s;
}

.view-button {
  background: #95a5a6;
  color: white;
}

.view-button:hover {
  background: #7f8c8d;
}

.edit-button {
  background: #3498db;
  color: white;
}

.edit-button:hover {
  background: #2980b9;
}

.status-button.active {
  background: #f39c12;
  color: white;
}

.status-button.upcoming {
  background: #2ecc71;
  color: white;
}

.status-button.ended {
  background: #3498db;
  color: white;
}

.delete-button {
  background: #e74c3c;
  color: white;
}

.delete-button:hover {
  background: #c0392b;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #95a5a6;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
  flex-wrap: wrap;
}

.pagination button {
  padding: 8px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
}

/* 对话框 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-large {
  max-width: 800px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #95a5a6;
}

.dialog-content {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #34495e;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 30px;
}

.cancel-button,
.save-button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.cancel-button {
  background: #ecf0f1;
  color: #7f8c8d;
}

.cancel-button:hover {
  background: #bdc3c7;
}

.save-button {
  background: #3498db;
  color: white;
}

.save-button:hover {
  background: #2980b9;
}

/* 活动详情样式 */
.activity-detail h2 {
  color: #2c3e50;
  margin-top: 0;
  margin-bottom: 15px;
}

.activity-desc {
  color: #34495e;
  line-height: 1.6;
  margin-bottom: 20px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 15px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.detail-label {
  font-weight: 500;
  color: #7f8c8d;
  min-width: 80px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .activities-container {
    padding: 10px;
  }
  
  .search-filter {
    flex-direction: column;
  }
  
  .filter-group {
    width: 100%;
  }
  
  .filter-group select {
    flex: 1;
  }
  
  .activities-table-container {
    overflow-x: auto;
  }
  
  .activities-table {
    display: block;
    overflow-x: auto;
  }
  
  .activities-table th,
  .activities-table td {
    padding: 8px;
    font-size: 13px;
  }
  
  .activity-name {
    max-width: 150px;
  }
  
  .action-buttons {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .pagination {
    flex-wrap: wrap;
  }
  
  .detail-grid {
    grid-template-columns: 1fr;
  }
}
</style>